
let xAppInfo = "vu63rd5psjallryc"//appid

import util from './x-util.js'

let commonHeaders = {
    "x-version": "1",
    "x-channel": "jy",
    "x-appInfo": xAppInfo,
    "x-sys-version": "1",
    "x-sys": "web",
    "x-p-version": "1.0.0",
    "x-traderName": "sec_app"
}

// let host=false?'http://127.0.0.1:9002':'https://dubaixing.qicp.vip'
// let host=location.port=='9998'?'http://localhost:9002':'https://dubaixing.qicp.vip'

// let dataCenterBaseUrl = 'https://dubaixing.qicp.vip/dataCenter'
// let iotApiBaseUrl = 'https://dubaixing.qicp.vip/iotapi'

// let dataCenterBaseUrl = 'http://10.251.80.150:32121/dataCenter'
// let iotApiBaseUrl = 'http://10.251.80.150:32121/iotapi'

let host = location.origin
// let host='http://127.0.0.1:7777'
// let host='http://122.9.13.38:7777'

if (location.port == '9998' || location.port == ''|| location.port == '81'|| location.port == '82'|| location.port == '8003') {
    // host = 'http://127.0.0.1:8004'
    // host = 'http://122.9.13.38:8061'
}

let dataCenterBaseUrl = host + '/dataCenter'
let iotApiBaseUrl = host + '/iotapi'


function getToken() {
    if(localStorage.getItem('xtoken')){
        return localStorage.getItem('xtoken')
    }

    // if(location.port=='9998'){
    //     return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC93707A6C9BB07B693B4B37651D44EE2CE7F08679CA3ACA23A7FFD9508416510507FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50E305ADA631D9FAA58169933A733B80BB7E0C958FC8F4C32093A21A9612C4FCD356DF58F3C498F5A32934C63BE098EF45'
    //     // return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC9D70BC3DC08F8BA4D05BAA60495E55772C31626FF279104FBB86A6EB67F46550B7FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50C53C3B2AE3592EFBC8CB13FE0DDE176BC68038BF05FDAE70C28331F57CBC22B8C4B0BE6089A60A1BBAB004521E2A0605'
    //     // return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC9EB5F0F274A69B773717854D06350D9D9030F935C55E43874CC3B101E95B5C35A7FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50837A7E962AE08E4327D542D6B404400FF03A53E154E123A8807519998D4CA4DC6DEDC2FCC61CE95FB54C6945413D95F4'
    //     // return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC9A908D12F2E7D0C5DAE0080E8CA7AEDB78462D8D30AF34EF3C879AF967E236EE27FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50F21640E8AC3897D8B03EC01AB07F572315A72E345C6A975D78B6DC9AD1C3C0B4DC72F1A00F2E9E75D1AE975D6C4BC8DA'
    //     // return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC9A9AB7C315236DA82EE203C2D93A637C6CF0173A80235967D9DE1B049952173FF7FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A5065E540F3B286BD62827DFE7AD05612B388D69B9BB31AE43DBF96FFAFBC500B55DF735C44B672DD494B67B5CA8EFD23B6'
    //     // return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC9AF507CCD4B5ECC7D73868D3C800C163D15A30D71DB22000C5535BD204D4A8BD07FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50F9AA4FD5014272742F0976BCE38315A0B0CD1FD3F6848E670F345A9A013E5A27A17E620243412C05C0EC37D6973F04BF'

    // }


    try {
        return document.cookie.split(';').find(s => s.startsWith('jy-token=')).split('jy-token=')[1]
    } catch (e) {
        return '9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC93707A6C9BB07B693B4B37651D44EE2CE7F08679CA3ACA23A7FFD9508416510507FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50E305ADA631D9FAA58169933A733B80BB7E0C958FC8F4C32093A21A9612C4FCD356DF58F3C498F5A32934C63BE098EF45'

        // return "9C04EBCB1143A177428C170ABB88E100826AE3A70A9B9EF71C3440D8E48CABC9255959D9BB41B0D1D2CC8931B231210B43ED2C27E8EF743BF46419BBD6A7C0287FB893217610673582D8893348D6BE8A3E2ADCE8C4DAA93E5C5DB922AB788A50CB2E6890D39BEA75B1163F5B535142037F22249DB7D18C27F8FFF6D4631165BD3567C825DE0FFA68923D90718E05E73D"
    }
}

commonHeaders['x-token'] = getToken();
window.parent.APP_CONFIG._HTTP_COMMON_HEADERS=commonHeaders
// console.log('commonHeaers--:',commonHeaders)


let request = (url, data, baseUrlType = '', method = 'post') => {
    url=host+url;
// let request = (url, data, baseUrlType = 'dataCenter', method = 'post') => {

    let p = {
        method: method,
        // mode:'cors',
        // credentials:'include',
        // body: JSON.stringify(data),
        headers: {
            'Content-Type': 'application/json',
            ...commonHeaders,
            // 'Authorization':'Bearer '+JSON.parse(window.parent.localStorage.getItem('currentUser')).token
        }
    }
    if (data) {
        p.body = JSON.stringify(data)
    }

    return fetch((baseUrlType == '' ? '' : '') + url, p).then(async function (data) {
        if(data.status!=200){

            return {
                httpCode:data.status,
                status:'fail'
            }
        }
        // debugger;
        // return fetch((baseUrlType=='dataCenter'?dataCenterBaseUrl:iotApiBaseUrl) + url+'?r='+Math.random(), p).then(async function (data) {
        let txt;
        try{
            txt=await data.text()
            data = {
                json:JSON.parse(txt),
                httpCode:200,
                message:'success'
            }
        }catch(e){
            data={
                json:txt,
                httpCode:200,
                message:'success'
            }
        }

        if (data?.message !== 'success') {
            ElementPlus.ElMessage.error(data.message)
            if (-999 == data.code) {
                if (window.parent !== window) {
                    window.parent.requestMessage({
                        data: {
                            msg: 'logout4App',
                            clientId: xAppInfo
                        }
                    })

                }
            }
            return []
        } else {
            return data
            // return data?.data
        }
        // console.log('data--:',data)


        // return data;
    })
}


let scadaApiBaseUrl='/iotapi/scadarest'
// let scadaApiBaseUrl='/scada'

let obj = {
    GetAllAlarmFile(gid){
        return request(scadaApiBaseUrl+'/api/File/GetAllAlarmFile'+'?groupId='+gid, null, '')
    },
    GetCompListBySceneId(id,projectId){
        return request(scadaApiBaseUrl+'/api/Mimic/'+projectId+'&r=69&id='+id, null, '','get')
    },
    DeleteAramFile(p){
        return request(scadaApiBaseUrl+'/File/DeleteAramFile?'+ util.obj2qs(p), null,'', 'delete')
    },
    bindWMX(p){
        return request(scadaApiBaseUrl+'/api/Variables/BindDataSource',p)
        // return request('/api​/Variables​/BindDataSource',p)
    },
    queryVarInfo(id,projectId){
        return request(scadaApiBaseUrl+'/api/Variables/QueryBind/' + projectId+'?veriableId='+id, null, '', 'get')
        // return request(scadaApiBaseUrl+'/api/Variables/QueryBind/' + id+'?projectId='+projectId, null, '', 'get')
    },
    fetchTree() {
        return request('/iot-config/conf/station/instance/list', {
            includeAll: false,
            pageNum: 1,
            pageSize: -1
        }, 'iot')
    },
    fetchSubTree(data) {
        return request('/iot-config/conf/station/instance/subunits', data, 'iot')
    },
    queryMultiHistory(arr, option) {
        return Promise.all(arr.map(s => obj.queryHistory(s, option || {

        })))

    },
    fetchDictsByModel(d) {
        // console.log('util-:',util)
        return request('/iot-config/conf/station/instance/unit/dict/list?' + util.obj2qs(d), null, 'iot', 'get')
    },
    fetchIndexDataBytationCode(code,stationCode){
        let d={
            deviceCode: code,
            stationCode: stationCode,
            keyword: ''
        }
        return request('/iot-config/data/governance/getIndexData?' + util.obj2qs(d), null, 'iot', 'get')
    },
    queryHistory(d = {}, option = {}) {
        // alert(1)

        let data = [
            // return request('/dataCenter/history/query',[
            {

                "endTime": 0,
                "option": {
                    // ["dataInterval" in d?"dataInterval":'foo']: d.dataInterval||0,
                    // "dataInterval": 0,

                    ...option,
                    // "pageNum": 1,
                    // "pageSize": 10000,
                    "dataInterval": 3600
                },
                ...d,
                // "startTime": 0,








                // "endTime": 1661529599000,
                // "startTime": 1661443200000,


                "endTime": 1653062400000,
                "startTime": 1652976030000,



                "viewType": "chart",
                "sourceType": "chart",
                // "viewType": "list",


                //list的
                // "stationCode": "ST_NTWZ",
                // "unitId": "ST_NTWZ",
                // "unitType": "GFCZ",


                //chart的

                "stationCode": "STXJBL",
                "unitId": "GFQ1XB10NBQ1NBDY1HLX1",
                "unitType": "HLX",

                //chart的
                "dictCode": "AGU",

                //  "dictCode": null,

            }
        ]
        if ('foo' in data[0].option) {
            delete data[0].option.foo
        }

        return request('/history/query', data)
    }
}

export default obj;